<template>
  <div class="app-container">
    <div style="margin-bottom: 20px;">
      <el-select v-model="listQuery.showRange" placeholder="请选择区域" clearable class="filter-item" style="width: 120px">
        <el-option label="全国" value="1" />
        <el-option label="地区" value="2" />
      </el-select>
      <el-cascader
        v-show="listQuery.showRange == 2"
        size="large"
        :options="areaOptions"
        @change="areaChange"
      />
      <el-select v-model="listQuery.typeId" placeholder="请选择类型" clearable class="filter-item" style="width: 140px">
        <el-option v-for="item in g.ad.adTypeList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-select v-model="listQuery.belongToPage" placeholder="请选择广告所属页面" clearable class="filter-item" style="width: 180px">
        <el-option v-for="item in g.ad.adTypePage" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-show="checkPermission('adAdd')" v-waves class="filter-item" type="primary" icon="el-icon-plus" style="float:right" @click="handleAdd">
        添加广告
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :element-loading-text="g.loadingStr"
      :header-cell-style="{background:g.mainColor}"
    >
      <el-table-column label="广告类型" align="center" width="160" fixed>
        <template slot-scope="{row}">
          <span>{{ row.typeId | adType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="广告名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="{row}">
          <span><img :src="row.imgUrl" alt="" class="table-row-img"></span>
        </template>
      </el-table-column>
      <el-table-column label="展示终端" align="center">
        <template slot-scope="{row}">
          <span>{{ row.belongToPage | adPageType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="展示位置" align="center">
        <template slot-scope="{row}">
          <span>{{ row.typeId | adType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投放范围" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.showRange | adAreaType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center">
        <template slot-scope="{row}">
          <span>
            开始：{{ row.beginTime | parseTime('{y}-{m}-{d} {h}:{i}') }}
            <br> 结束：{{ row.endTime | parseTime('{y}-{m}-{d} {h}:{i}') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.enable === 1 ? '启用' : '禁用' }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="160px">
        <template slot-scope="{row,$index}">
          <el-button v-if="row.enable === 0" type="success" size="mini" @click="handleStatus(row,$index)">
            启用
          </el-button>
          <p />
          <el-button v-if="row.enable === 1" type="danger" size="mini" @click="handleStatus(row,$index)">
            禁用
          </el-button>
          <p />
          <el-button v-if="row.enable === 0 && checkPermission('adEdit')" type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <p />
          <el-button v-if="row.enable === 0" type="danger" size="mini" @click="handleDel(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>

import { provinceAndCityData, CodeToText } from 'element-china-area-data'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { adList, adDel, adEdit } from '@/api/ad'

export default {
  name: 'AdList',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        provinces: '',
        citys: '',
        showRange: '1',
        typeId: '1',
        belongToPage: null
      },
      areaOptions: provinceAndCityData
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      if (this.listQuery.showRange === '1') {
        this.listQuery.provinces = null
        this.listQuery.citys = null
      }
      adList(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    areaChange(value) {
      const provinces = CodeToText[value[0]]
      const city = CodeToText[value[1]]
      this.listQuery.provinces = provinces
      this.listQuery.citys = city
    },
    handleStatus(row, index) {
      this.$confirm('是否确认' + (row.enable === 0 ? '启用' : '禁用') + '该广告？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(JSON.stringify(this.multipleSelection))
        adEdit({
          id: row['id'],
          enable: 1 - row.enable
        }).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list[index]['enable'] = 1 - row.enable
        })
      })
    },
    handleUpdate(row) {
      console.log(row)
      this.$router.push({ name: 'AdEdit', params: { id: row.id }})
    },
    handleDel(row, index) {
      console.log(row)

      this.$confirm('是否确认删除该广告？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(JSON.stringify(this.multipleSelection))
        adDel({
          id: row['id']
        }).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list.splice(index, 1)
        })
      })
    },
    handleAdd() {
      this.$router.push('AdAdd')
    }
  }
}
</script>
